package com.weetop.cfw.mine.activity

import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.king.app.dialog.AppDialog
import com.king.app.dialog.AppDialogConfig
import com.king.app.updater.AppUpdater
import com.king.app.updater.http.OkHttpManager
import com.weetop.cfw.R
import com.weetop.cfw.base.presenter.imp.VersionInfoPresenterImp
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.view.VersionInfoView
import com.weetop.cfw.bean.AppVersionBean
import com.weetop.cfw.bean.SinglePageIntroduceBean
import com.weetop.cfw.constants.Constants
import com.weetop.cfw.constants.UrlConstants
import com.weetop.cfw.other.CommonWebViewActivity
import com.weetop.cfw.other.RichTextWebViewActivity
import kotlinx.android.synthetic.main.activity_version_info.*


/**
 * 版本号界面
 */
class VersionInfoActivity : CommonBaseActivity(), View.OnClickListener, VersionInfoView {

    private lateinit var versionInfoPresenterImp: VersionInfoPresenterImp

    private var appFuncStr = ""//APP功能介绍富文本

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.imageVersionInfoBack -> ActivityUtils.finishActivity(this)
            R.id.relativeCheckNewVersion -> versionInfoPresenterImp.checkAppVersionInfo(this)
            R.id.relativeFunctionIntroduction -> RichTextWebViewActivity.startRichTextWebViewActivity(
                this,
                "功能介绍",
                UrlConstants.function_introduction_web_url,
                true
            )
            R.id.relativeComplaint -> ActivityUtils.startActivity(FeedBackActivity::class.java)
            R.id.textSoftwareLicenseAndServiceAgreement -> CommonWebViewActivity.startCommonWebViewActivity(
                this,
                UrlConstants.software_license_and_service_agreement_web_url
            )
            R.id.textPrivacyPolicy -> CommonWebViewActivity.startCommonWebViewActivity(
                this,
                UrlConstants.privacy_policy_web_url
            )
        }
    }

    override fun getLayoutId() = R.layout.activity_version_info

    override fun initView(savedInstanceState: Bundle?) {
        setViewsOnClickListener(
            this,
            imageVersionInfoBack,
            relativeCheckNewVersion,
            relativeFunctionIntroduction,
            relativeComplaint,
            textSoftwareLicenseAndServiceAgreement,
            textPrivacyPolicy
        )
    }

    override fun initData(savedInstanceState: Bundle?) {
        versionInfoPresenterImp = VersionInfoPresenterImp()
        versionInfoPresenterImp.attachView(this)

        versionInfoPresenterImp.getAppFunctionIntroduction(this)
    }

    /**
     * APP功能介绍富文本获取成功
     */
    override fun appFunctionIntroductionGetSuccess(singlePageIntroduceBean: SinglePageIntroduceBean?) {
        if (singlePageIntroduceBean != null) {
            appFuncStr = singlePageIntroduceBean.content
        }
    }

    /**
     * App版本信息获取成功
     */
    override fun appVersionInfoGetSuccess(appVersionBean: AppVersionBean?) {
        if (appVersionBean != null) {
            if (appVersionBean.hasnew == 1) {//有最新版本
                val config = AppDialogConfig()
                config.apply {
                    title = "APP更新"
                    ok = "立即更新"
                    cancel = "暂不更新"
                    content = ""
                    onClickOk = View.OnClickListener {
                        AppUpdater.Builder()
                            .serUrl("${UrlConstants.baseUrl}${appVersionBean.verfile}")
                            .setFilename("mhcf.apk")
                            .build(this@VersionInfoActivity)
                            .setHttpManager(OkHttpManager.getInstance())//使用OkHttpClient实现下载，需依赖okhttp库
                            .start()
                        AppDialog.INSTANCE.dismissDialogFragment(supportFragmentManager)
                    }
                    onClickCancel = View.OnClickListener {
                        AppDialog.INSTANCE.dismissDialogFragment(supportFragmentManager)
                    }
                }
                AppDialog.INSTANCE.showDialogFragment(supportFragmentManager, config);
            } else {
                Toast.makeText(this@VersionInfoActivity, "已是最新版本", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        versionInfoPresenterImp.detachView()
    }

}
